#include "ringQueue.hpp"
#include "task.hpp"
#include <cstdlib>
#include <ctime>
#include <unistd.h>

void* product(void* args)
{
    ringQueue<Task>* rq = (ringQueue<Task>*)args;
    while (true)
    {
        //v1
        // int data = rand() % 10 + 1;
        // rq->push(data);
        // cout << "productor: " << data << endl;

        //v2
        int x = rand() % 10 + 1;
        int y = rand() % 10 + 1;
        int opIndex = rand() % oper.size();
        Task t(x, y, oper[opIndex], Compute);
        rq->push(t);
        cout << "productor: " << t.toTaskString() << endl;

        //sleep(1);//生产慢消费快 生产一条消费一条
    }
    return nullptr;
}

void* consume(void* args)
{
    ringQueue<Task>* rq = (ringQueue<Task>*)args;
    while (true)
    {
        //v1
        // int data;
        // rq->pop(&data);
        // cout << "consumer: " << data << endl;

        //v2
        Task t;
        rq->pop(&t);
        cout << "consumer: " << t() << endl;

        //sleep(1);//消费慢生产快 生产一批 消费一条 队列满后 生产一条消费一条
    }
    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));
    //ringQueue<int> rq;
    ringQueue<Task> rq;
    pthread_t productor, consumer;
    pthread_create(&productor, nullptr, product, &rq);
    pthread_create(&consumer, nullptr, consume, &rq);

    pthread_join(productor, nullptr);
    pthread_join(consumer, nullptr);
    return 0;
}